package kaoshi.servlet;

import kaoshi.dao.impl.personDaoImpl;
import kaoshi.dao.personDao;
import kaoshi.entity.person;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/chaxvnServlet")
public class chaxvnServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        personDao dao = new personDaoImpl();
        int totalcount = dao.getByPaged();//拿到总数量据量
        //计算总页数
        Integer pageSize = null;//用来储存总页数
        if (totalcount%5==0){//能被整除的情况
            //总页数=总数据量/每页展示的数据量
            pageSize = (totalcount/5);
        }else if(totalcount%5!=0){//不能整除的情况
            //总页数=（总数据量/每页展示的数据量）
            pageSize = (totalcount/5)+1;
        }
        //拿到请求中携带的代码
        String page = request.getParameter("pageNo");
        Integer pageNo = null;//用来储存当前页码
        if (null==page){//如果页码为空  表示默认查询第一页
            pageNo=1;
        }else{//否则  转成int类型
            pageNo = Integer.parseInt(page);
        }

        personDao person = new personDaoImpl();
        List<person> list = person.getByPage(pageNo,5);
        //把当前页码  放到作用域
        request.setAttribute("pageNo",pageNo);
        request.setAttribute("pageSize",pageSize);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/jsp/Login_1.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
